/* **************************************************************************/
/*COPYRIGHT CCWIN-IN ALL RIGHTS RESERVED. THIS IS AN UNPUBLISHED WORK.      */
/*V8:ConvertMode=Report                                                     */
/*V8:WebEnabled=Yes                                                         */
/*V8:RunMode=Character,Windows                                              */
/*EXTRACTING DATA FROM MFG/PRO                                              */
/****************************************************************************/
/* PROCEDURE NAME        : xxsuppliercrtapi.p                               */
/* PROCEDURE TYPE        : supplier API Process                             */
/* DESCRIPTION           : supplier Create API Process                      */
/*                                                                          */     
/* INCLUDE FILES         :                                                  */
/* CALLED BY             :                                                  */
/* CALLED PROCEDURES     :                                                  */
/****************************************************************************/
/* CREATED BY            : Gaoyan                               04-Jul-20   */
/* CREATED BY            : Gaoyan                               21-OCT-24   */
/****************************************************************************/ 
/****************************************************************************/
/****************************************************************************/
{us/bbi/mfdeclre.i}
{us/wb/wbrp01.i}         
{us/gl/glsec.i} 
{us/bbi/gplabel.i}


Define input  parameter m_traceid            as character no-undo.
define input  parameter m_import             as logical   no-undo.
Define output parameter m_status             as logical   no-undo.
Define output parameter m_errmsg             as character no-undo.


define variable v_apiresult           as character                no-undo.
define variable v_id                  as integer  init 0 no-undo.
define variable v_errflag             as logical   initial yes    no-undo.
define variable v_curtaskid           as character                no-undo.
define variable v_cnt                 as integer                  no-undo.
define variable m_error               as integer                  no-undo.
define variable m_wrng                as integer                  no-undo.


/* ======== for api ========= */
define variable vireturn                     as integer     no-undo.
define variable ilreturndataset              as logical     no-undo.
define variable ilpartialupdate              as logical     no-undo.
define variable icpartialupdateexceptionlist as character   no-undo.
define variable ocprimarykey                 as character   no-undo.
define variable ocrowid                      as character   no-undo.
define variable oidraftinstance              as integer     no-undo.
define variable ocprimarykeyname             as character   no-undo.
define variable vho                          as handle.
define variable oireturnstatus               as integer     no-undo. 
define variable m_company                    like company.companycode no-undo.
define variable m_busnrelid1                 as integer no-undo.
define variable m_addid                      as integer no-undo.
define variable m_length                     as integer no-undo.

{ proxy/bcreditordef.i} 

define shared temp-table tt_tcreditor like tcreditor
                  field tt_daybook as character
                  field tt_site    as character
                  field tt_traceid as character
                  field tt_dataid as character.
define shared temp-table tt_tcreditorvat      like tcreditorvat.
define shared temp-table tt_tfcmessages       like tfcmessages.


/*Define temp-table tBankNumberRefCreditor like BankNumber.*/
Define temp-table tBankNumberRefCreditor no-undo 
       field BankNumber_ID as integer
       field BankBusinessRelation_ID as integer
       field ParentObject_ID as integer
       field Currency_ID as integer
       field BankPayFormat_ID as integer
       field CompanySharedSet_ID as integer
       field BankAccFormat_ID as integer
       field DDocumentStatus_ID as integer
       field BankNumberBankChargeGL_ID as integer
       field BankNumber as character
       field BankNumberFormatted as character
       field BankNumberIsActive as logical
       field BankNumberIsDefault as logical
       field BankNumberExtension as character
       field BankNumberValidation as character
       field BankNumberSwiftCode as character
       field BankNumberParentType as character
       field BankNumberBranch as character
       field BankNumberIsSBDef as logical
       field BankNumberIsBankCharge as logical
       field BankNumberBankAccountType as character 
       field tcParentObjectCode as character
       field tcBankBusinessRelationCode as character
       field tcCurrencyCode as character
       field tcCompanyCode as character
       field tcOwnBankNumber as character
       field tcOwnGLCode as character
       field tcPayFormatTypeCode as character
       field tcPayFormatTypePaymentInstrument as character
       field tlBankNumberIsReferenced as logical init "no"
       field tcBankAccFormatCode as character
       field tcDDocumentStatusTR as character
       field tlExistRecord as logical
       field tcBankChargeGLCode as character
       field CustomShort0 as character
       field CustomShort1 as character
       field CustomShort2 as character
       field CustomShort3 as character
       field CustomShort4 as character
       field CustomShort5 as character
       field CustomShort6 as character
       field CustomShort7 as character
       field CustomShort8 as character
       field CustomShort9 as character
       field CustomCombo0 as character
       field CustomCombo1 as character
       field CustomCombo2 as character
       field CustomCombo3 as character
       field CustomCombo4 as character
       field CustomCombo5 as character
       field CustomCombo6 as character
       field CustomCombo7 as character
       field CustomCombo8 as character
       field CustomCombo9 as character
       field CustomLong0 as character
       field CustomLong1 as character
       field CustomNote as character
       field CustomDate0 as date
       field CustomDate1 as date
       field CustomDate2 as date
       field CustomDate3 as date
       field CustomDate4 as date
       field CustomInteger0 as integer init ?
       field CustomInteger1 as integer init ?
       field CustomInteger2 as integer init ?
       field CustomInteger3 as integer init ?
       field CustomInteger4 as integer init ?
       field CustomDecimal0 as decimal decimals 4
       field CustomDecimal1 as decimal decimals 4
       field CustomDecimal2 as decimal decimals 4
       field CustomDecimal3 as decimal decimals 4
       field CustomDecimal4 as decimal decimals 4
       field LastModifiedDate as date
       field LastModifiedTime as integer
       field LastModifiedUser as character
       field QADC01 as character
       field QADC02 as character
       field QADT01 as date
       field QADD01 as decimal decimals 10
       field tc_Rowid as character
       field tc_ParentRowid as character
       field tc_Status as character
       field tBankNumberRefCreditor_copy   as Logical
       field taccount_indicator   as Character
       field tBankNumberRefCreditor_sort as Character
       index i_parent tc_ParentRowid
       index i_status tc_Status
       index prim is unique is primary tc_Rowid.


define dataset tBankNumberRefCreditor for tBankNumberRefCreditor.


	 empty temp-table  tfcmessages.
	 empty temp-table  tcreditor.
	 empty temp-table  tcreditorvat.
	 empty temp-table  tbanknumberrefcreditor.
	 
	for each tt_tcreditor no-lock where tt_tcreditor.tt_traceid = m_traceid:  

    create tcreditor.
    buffer-copy tt_tcreditor except tt_dataid tt_traceid to tcreditor.
    
    assign
        tcreditor.creditor_id  = next-value(objectnumber).
		
        for first Profile no-lock where ProfileCode = string(tt_tcreditor.tcInvControlGLProfileCode) :
        Assign 
              tCreditor.InvControlGLProfile_ID      = Profile.Profile_ID
              tCreditor.tcInvControlGLProfileCode   = Profile.ProfileCode.
        end. /* first Profile */
		if not available profile then 
		tCreditor.tc_status = "Inv PROFILE-FILE error".
        for first Profile no-lock where ProfileCode = string(tt_tcreditor.tccncontrolglprofilecode) :
		assign
		        tcreditor.cncontrolglprofile_id      = Profile.profile_id
                tcreditor.tccncontrolglprofilecode   = Profile.profilecode.
		end.
		if not available Profile then 
		tCreditor.tc_status = tCreditor.tc_status + "|" + "CreditNote PROFILE-FILE error".
		for first Profile no-lock where ProfileCode = string(tt_tcreditor.tcprepaycontrolglprofilecode) :
		assign
		       tcreditor.prepaycontrolglprofile_id    = Profile.profile_id
               tcreditor.tcprepaycontrolglprofilecode = Profile.profilecode.
		end.
		if not available Profile then 
		tCreditor.tc_status = tCreditor.tc_status + "|" + "Prepayment PROFILE-FILE error".
		for first Profile no-lock where ProfileCode = string(tt_tcreditor.tcPurchaseGLProfileCode):		
        Assign 	
		       tcreditor.PurchaseGLProfile_ID  = profile.profile_id
			   tcreditor.tcPurchaseGLProfileCode = profile.profilecode.
        end.
		if not available Profile then 
		tCreditor.tc_status = tCreditor.tc_status + "|" + "Purchase PROFILE-FILE error".
		for first Profile no-lock where ProfileCode = string(tt_tcreditor.tcDivisionProfileCode):		
        Assign 	
		       tcreditor.DivisionProfile_ID  = profile.profile_id
			   tcreditor.tcDivisionProfileCode = profile.profilecode.
        end.		
		
        /* tCreditor.tc_status = tCreditor.tc_status + "|" + "SubAccountProfile error". */
        for first Reason where reason.reasoncode = tt_tcreditor.tcReasonCode no-lock:
            assign 
               tCreditor.Reason_ID        = reason.reason_ID
               tCreditor.tcReasonCode     = reason.reasoncode.
        end. /*first Reason*/
        if not available Reason then 
		tCreditor.tc_status = tCreditor.tc_status + "|" + "Invoice status code error".
	  
	
	  
/**** Currently there is no vat data with supplier in BMPT and FS ****/

/*    v_id = next-value(objectnumber).
   for first domains where domaincode = global_domain no-lock:
   end. /*domains*/

   if avail domains then
   do:
       for first vat where vat.domain_id = domains.domain_id and vatcode = m3_wthtxcd and vatisactive no-lock :
           assign
              m_vatid     = vat_id
              m_vatinout  = vatinout.
       end.  /*vat*/
       if not avail vat then
       do:
           {us/bbi/pxmsg.i &msgnum=111 &errorlevel=1 &msgbuffer=m3_errdsc  } 
            assign
              m3_err        = yes
              m3_errdsc     = m3_errdsc .
              return.
       end. /*not avail vat*/
   end.
   create tcreditorvat.
   assign
          tcreditorvat.creditorvat_id  = v_id
          tcreditorvat.creditor_id     = tcreditor.creditor_id
          tcreditorvat.vat_id          = 
          tcreditorvat.tcvatcode       =  
          tcreditorvat.tcdomaincode    = global_domain
          tcreditorvat.tcvatinout      = 
          tcreditorvat.tc_parentrowid  = tcreditor.tc_rowid
          tcreditorvat.tc_rowid        = "vatline". */

/*  bmpt will create supplier bank 
       create tbanknumberrefcreditor.
       assign
               tbanknumberrefcreditor.banknumber_id                       = 
               tbanknumberrefcreditor.parentobject_id                     = 
               tbanknumberrefcreditor.bankbusinessrelation_id             = 
               tbanknumberrefcreditor.bankpayformat_id		              =  
               tbanknumberrefcreditor.banknumberbankchargegl_id	          =
               tbanknumberrefcreditor.banknumber                          = 
               tbanknumberrefcreditor.banknumberformatted                 = 
               tbanknumberrefcreditor.banknumberswiftcode                 = 
               tbanknumberrefcreditor.tcbankaccformatcode                 = 
               tbanknumberrefcreditor.tcpayformattypecode                 = 
               tbanknumberrefcreditor.tcpayformattypepaymentinstrument    = 
               tbanknumberrefcreditor.tcparentobjectcode                  = 
               tbanknumberrefcreditor.tccurrencycode                      = 
               tbanknumberrefcreditor.tccompanycode                       = 
               tbanknumberrefcreditor.tcownbanknumber                     = 
               tbanknumberrefcreditor.tc_rowid                            = 
               tbanknumberrefcreditor.tc_parentrowid                      = 
               tbanknumberrefcreditor.banknumberisactive                  = 
               tbanknumberrefcreditor.tcbankbusinessrelationcode          = 
               tbanknumberrefcreditor.banknumberbranch                    = 
               tbanknumberrefcreditor.tbanknumberrefcreditor_copy         = 
               tbanknumberrefcreditor.banknumberbankaccounttype           = 
               tbanknumberrefcreditor.taccount_indicator                  = 
               tbanknumberrefcreditor.companysharedset_id                 = 
               tbanknumberrefcreditor.banknumberextension                 = 
               tbanknumberrefcreditor.customlong0                         = 
               tbanknumberrefcreditor.customshort0                        = 
               tbanknumberrefcreditor.tcbankbusinessrelationcode          = 
               ttbnk_m_bnkkey                                             = 
               . */	  
	  
	  
   if m_import
   then do:  
	/*start proxy persistently*/
    run proxy/bcreditor.p persistent set vhproxycomponent.
    run apimaintainbydatasetwithoutput in vhproxycomponent
        ('',
         'save',
         true,
         true,
         '',
         dataset bcreditor by-reference,
         output ocprimarykey,
         output ocrowid,
         output oidraftinstance,
         output ocprimarykeyname,
         output dataset-handle vho,
         output dataset tfcmessages,
         output vireturn).
		 
      for each tfcmessages no-lock:
		create tt_tfcmessages.
		buffer-copy tfcmessages to tt_tfcmessages.   
	  end.
	if vireturn = 0 then 
	assign m_error  = 0
	       m_status = no.
    /* determine error status */		 
    if vireturn <> 0 
    then do:
        assign
            m_error = 0
            m_status = yes.
        for each tfcmessages no-lock:

            if tfcmessages.tcfctype = "e" then
            do:
                 m_error = -1.
     			 v_cnt = v_cnt + 1.
    			   if tfcmessages.tcfcfieldname <> "" then
                      v_apiresult = v_apiresult  + "|" + 
    					                     gettermlabel("severity",8) + " = " +
                                             string(tfcmessages.tifcseverity) + " | " +
                                             tfcmessages.tcfcmessage + " | " +
                                             gettermlabel("field_name",10) + " = " +
                                             tfcmessages.tcfcfieldname + " | " +
                                             gettermlabel("field_label",11) + " = " +
                                             tfcmessages.tcfcfieldlabel + " | " +
                                             gettermlabel("field_value",11) + " = " +
                                             tfcmessages.tcfcfieldvalue + " | " +
                                             tfcmessages.tcfcmsgnumber.
                       else
                       v_apiresult  = v_apiresult  + "|" + 
        					                  gettermlabel("severity",8) + " = " +
                                              string(tfcmessages.tifcseverity) + " | " +
                                              tfcmessages.tcfcmessage + " | " +
                                              tfcmessages.tcfcmsgnumber.
                        
                    /*  message v_apiresult view-as alert-box. */
    				
                     if v_cnt > 200 then leave.            
    	 
            end. /* if tfcmessages.tcfctype = "e" then */
    
            if tfcmessages.tcfctype = "w" then 
            do:
                m_wrng = -1.
            end. /*if warning*/
        end. /* for each tfcmessages no-lock */
        
    end. /* vireturn <> 0 */
    
	    /* api procedure end */
    delete procedure vhproxycomponent.
    
    run proxy/stopcbsession.p (output dataset tfcmessages, output vireturn).
	
	
	end. /* IF m_import */
    
	if can-find(first tcreditor where tcreditor.tc_status <> "")
	then do:
	   m_error = -99.
	   m_status = yes.
	   
	end.
	
	m_errmsg = v_apiresult.

	end. /* for first tes_supplier */